var mongoose = require('mongoose')
var options = {
  useMongoClient: true,
  autoIndex: false, // Don't build indexes
  reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
  reconnectInterval: 500, // Reconnect every 500ms
  poolSize: 10, // Maintain up to 10 socket connections
  // If not connected, return errors immediately rather than waiting for reconnect
  bufferMaxEntries: 0
}
// mongoose.connect('mongodb://test:123456@localhost/test')
mongoose.connect('mongodb://test:123456@127.0.0.1:27017/test')

var db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', function() {
  console.log('connection is opened!')

  var kittySchema = mongoose.Schema({
    name: String
  })

  kittySchema.methods.speak = function() {
    var greeting = this.name ? 'Meow name is' + this.name : 'I don\'t have a name'
    console.log(greeting)
  }

  var Kitten = mongoose.model('Kitten', kittySchema);

  var felyne = new Kitten({name: 'Felyne'})
  console.log(felyne.name)

  var fluffy = new Kitten({ name: 'fluffy' });
  fluffy.speak()

  fluffy.save(function (err, fluffy) {
    if (err) return console.error(err)
    fluffy.speak()
    // db.close()
  })

  Kitten.find(function(err, kittens) {
    if (err) return console.error(err)
    console.log(kittens)
    // db.close()
  })

  Kitten.find({ name: /^fluff/ }, function(err, kittens) {
    if (err) return console.error(err)
    console.log(kittens)
    // db.close()
  });
})
